# -*- coding: utf-8 -*-
"""
Created on Tue Apr 02 16:22:25 2013

@author: Maxim
"""
import propeller
import matplotlib.pyplot as plt
import matplotlib as mpl
from numpy import linspace, zeros
import dbTools

def read_exp_data():
    dataPath = r'D:\engauge\propeller\digitized_results.xls'
    wb = dbTools.loadDB(dataPath)
    db = dbTools.readDB(wb.selectByName('NACA5868-9'))
    J = list()
    CP = list()
    CT = list()
    
    J.append(db.readCol(2,0,10))
    J.append(db.readCol(2,3,13))
    J.append(db.readCol(2,6,13))
    J.append(db.readCol(2,9,13))
    CP.append(db.readCol(2,1,10))
    CP.append(db.readCol(2,4,13))
    CP.append(db.readCol(2,7,13))
    CP.append(db.readCol(2,10,13))
    CT.append(db.readCol(2,2,10))
    CT.append(db.readCol(2,5,13))
    CT.append(db.readCol(2,8,13))
    CT.append(db.readCol(2,11,13))
    return J, CP, CT

def validation():
    name = 'NACA5868'

    beta = [15.,25.,35.,45.]
    rpm  = [1200.,1000.,800.,800.]
    V = linspace(5.,75,50)
    rho = 1.2255
    
    color = ['k','b','r','m']
    
    font = {'family' : 'sans-serif',
            'weight' : 'normal',
            'size'   : 18}
    
    mpl.rc('font',**font)
    
    J  = zeros([len(beta),len(V)])
    CP = zeros([len(beta),len(V)])
    CT = zeros([len(beta),len(V)])
    
    Jexp, CPexp, CTexp = read_exp_data()
    
    prop = propeller.propeller()
    prop.read_xls(name)
    
   
    for i,b in enumerate(beta):
        for j,v in enumerate(V):
            data = prop.analyze_prop(b,rpm[i],v,rho)
            J[i,j] = data[2]
            CT[i,j] = data[3]
            CP[i,j] = data[4]

    dx = 0.01
    dy = 0.005
    plt.figure(1)
    plt.hold(True)
    plt.grid(True)
    for i in range(len(beta)):
        plt.plot(J[i,:],CP[i,:],color[i]+'-')
        plt.plot(Jexp[i],CPexp[i],color[i]+'s')
        plt.text(Jexp[i][5]+dx,CPexp[i][5]+dy,'%.0fdeg'%beta[i])
    plt.xlabel('J = V/nD')
    plt.ylabel('CP')
    plt.title('Power coefficient validation')
    plt.legend(['computed','experimental'])
    plt.axis([0,3.0,0,0.2])

    plt.figure(2)
    plt.hold(True)
    plt.grid(True)
    for i in range(len(beta)):
        plt.plot(J[i,:],CT[i,:],color[i]+'-')
        plt.plot(Jexp[i],CTexp[i],color[i]+'s')
        plt.text(Jexp[i][7]+dx,CTexp[i][7]+dy,'%.0fdeg'%beta[i])
    plt.axis([0,3.0,0,0.15])
    plt.xlabel('J = V/nD')
    plt.ylabel('CT')
    plt.title('Thrust coefficient validation')
    plt.legend(['computed','experimental'])
    plt.show()

if __name__=="__main__":
    validation()